﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using WebMatrix.WebData;

namespace qkview.Models
{
    public class Common
    {
        public static string DefaultColorCode = "#D4FCFC";

        public static string Encode(string encodeMe)
        {
            byte[] encoded = System.Text.Encoding.UTF8.GetBytes(encodeMe);
            return Convert.ToBase64String(encoded);
        }

        public static string Decode(string decodeMe)
        {
            byte[] encoded = Convert.FromBase64String(decodeMe);
            return System.Text.Encoding.UTF8.GetString(encoded);
        }

        /// <summary>
        /// To generate URL Slug from string
        /// </summary>
        /// <param name="phrase"></param>
        /// <returns></returns>
        public static string GenerateSlug(string phrase)
        {
            string str = RemoveAccent(phrase).ToLower();
            // invalid chars           
            str = Regex.Replace(str, @"[^a-z0-9\s-]", "");
            // convert multiple spaces into one space   
            str = Regex.Replace(str, @"\s+", " ").Trim();
            // cut and trim 
            str = str.Substring(0, str.Length <= 45 ? str.Length : 45).Trim();
            str = Regex.Replace(str, @"\s", "-"); // hyphens   
            return str;
        }

        public static string RemoveAccent(string txt)
        {
            byte[] bytes = System.Text.Encoding.GetEncoding("Cyrillic").GetBytes(txt);
            return System.Text.Encoding.ASCII.GetString(bytes);
        }

        public static UserSetting GetUserSetting()
        {
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
            {
                return null;
            }
            UserSetting ret;
            if (HttpContext.Current.Session["setting"] == null)
            {
                using (QKVEntities db = new QKVEntities())
                {
                    ret = db.UserSettings.FirstOrDefault(x => x.UserProfile.UserName == HttpContext.Current.User.Identity.Name);
                    if (ret == null) {
                        var setting = new UserSetting()
                        {
                            TotalBoards = Convert.ToInt32(ConfigurationManager.AppSettings["TotalBoards"]),
                            TotalNotes = Convert.ToInt32(ConfigurationManager.AppSettings["TotalNotes"]),
                            TotalSection = Convert.ToInt32(ConfigurationManager.AppSettings["TotalSections"]),
                            AdvSecDescription = Convert.ToBoolean(Convert.ToInt32(ConfigurationManager.AppSettings["AdvSecDescription"])),
                            UserID = WebSecurity.CurrentUserId,
                            LastModified = DateTime.UtcNow
                        };
                        db.UserSettings.Add(setting);
                        db.SaveChanges();
                        ret = setting;
                    }
                }
                HttpContext.Current.Session["setting"] = ret;
            }
            else
            {
                ret = (UserSetting)HttpContext.Current.Session["setting"];
            }
            return ret;
        }


    }
}